<?php


namespace api\zmt\controller;

    use cmf\controller\RestBaseController;
    use think\facade\Db;
    use think\Request;


class OrderController extends RestBaseController
{
    public function createOrder(Request $request)
    {
        $data = input('post.');
        $user_id = $request->user_id;
        $goods = Db::name('goods')
            ->where('id','=',$data['goods_id'])
            ->find();
        $address = Db::name('address')->where('user_id','=',$user_id)
            ->find();
        $insert_data =[];
        $trade_no = date('YmdHis').mt_rand(1111,9999);
        $insert_data[]=[
            'user_id'=>$user_id,
            'goods_name'=>$goods['name'],
            'goods_info'=>json_encode($data['goods_info']),
            'price'=>$goods['freight'],
            'num'=>$data['num'],
            'total_price'=>$goods['freight'] * $data['num'],
            'trade_no'=>$trade_no,
            'user_name'=>$address['address_user'],
            'user_address'=>$address['address'],
            'user_tel'=>$address['mobile'],
            'status'=>0,
            'remarks'=>$data['remarks'],
            'create_time'=>date('Y:m:d H:i:s')
        ];

        $res = Db::name('order')
            ->insertAll($insert_data);
        if($res){
            $this->success('订单创建成功',['order_id'=>$trade_no]);
        }else{
            $this->error('失败');
        }
    }
}